<template>
  <div class="qrcode-container">
    <canvas ref="qrCanvas"></canvas>
  </div>
</template>

<script setup>
import { ref, onMounted, watch } from 'vue'
import QRCode from 'qrcode'

const props = defineProps({
  text: {
    type: String,
    required: true
  },
  size: {
    type: Number,
    default: 200
  },
  color: {
    type: String,
    default: '#000000'
  }
})

const qrCanvas = ref(null)

const generateQRCode = async () => {
  try {
    await QRCode.toCanvas(qrCanvas.value, props.text, {
      width: props.size,
      margin: 1,
      color: {
        dark: props.color,
        light: '#ffffff'
      }
    })
  } catch (error) {
    console.error('生成二维码失败:', error)
  }
}

onMounted(() => {
  generateQRCode()
})

watch(() => props.text, () => {
  generateQRCode()
})
</script>

<style scoped>
.qrcode-container {
  display: flex;
  justify-content: center;
  align-items: center;
}
</style> 